package com.sy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sy.dto.PageBean;
import com.sy.entity.Question;
import com.sy.entity.Student;
import com.sy.entity.Teacher;
import com.sy.exception.BusinessException;
import com.sy.mapper.QuestionMapper;
import com.sy.mapper.StudentMapper;
import com.sy.mapper.TeacherMapper;
import com.sy.service.QuestionService;
import com.sy.service.TeacherService;
import com.sy.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author:fma
 * @Date: 2021/10/15 10:36
 */
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private TeacherService teacherService;


    @Override
    public PageBean<Question> getQuestisonBySno(StudentPageVo studentPageVo) {
        Integer sno = studentPageVo.getSno();
        if (Objects.isNull(sno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        Student stuPwdByName = studentMapper.getStuPwdByName(sno);
        if (Objects.isNull(stuPwdByName)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "该学生未找到...");
        }
        PageHelper.startPage(studentPageVo.getPageNo(), studentPageVo.getPageNum());
        Page<Question> page = (Page<Question>) questionMapper.getQuestionBySno(sno);
        PageBean<Question> pageBean = new PageBean<>();
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(studentPageVo.getPageNum());
        pageBean.setTotal(page.getTotal());
        List<Question> products = page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;
    }

    @Override
    public PageBean<Question> getQuestionByTno(StuAndTnoVo stuAndTnoVo) {
        Integer sno = stuAndTnoVo.getSno();
        if (Objects.isNull(sno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        String teacherName = stuAndTnoVo.getName();
        if (StringUtils.isBlank(teacherName)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师的姓名不能为空");
        }
        Teacher teacher = teacherMapper.getTeacherByName(teacherName);
        if (Objects.isNull(teacher)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "教师未找到...");
        }
        stuAndTnoVo.setTno(teacher.getTno());
        Integer pageNo = stuAndTnoVo.getPageNo();
        Integer pageNum = stuAndTnoVo.getPageNum();
        PageHelper.startPage(pageNo, pageNum);
        Page<Question> page = (Page<Question>) questionMapper.getQuestionByTno(stuAndTnoVo);
        PageBean<Question> pageBean = new PageBean<>();
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        List<Question> questions = page.getResult();
        pageBean.setRecords(questions.stream().collect(Collectors.toList()));
        if (pageBean.getRecords().size() == 0) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "你未选择该教师...");
        }
        return pageBean;
    }

    @Override
    public Integer getQuestionCount(Integer sno) {
        if (Objects.isNull(sno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        List<Question> questions = questionMapper.getQuestionBySno(sno);
        if (Objects.isNull(questions)) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "该学生作业未找到...");
        }
        return questions.size();
    }

    //这段要检查
    @Override
    public PageBean<Question> getQuestionsLikeName(Integer sno, String teacherName , Integer pageNo ,Integer pageNum) {
        if (Objects.isNull(sno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        if (StringUtils.isBlank(teacherName)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "关键字不能为空");
        }
        //这是该学生的全部作业
        List<Question> questions = questionMapper.getQuestionBySno(sno);
        if (questions.isEmpty()) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "该学生作业未找到...");
        }
        //这是查询后教师的总编号
        List<Integer> teachersTno = teacherService.getTeacherByLikeName(teacherName);
        if (teachersTno.isEmpty()) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "老师未存在或您未选择该老师...");
        }
        QuestionAndTno questionAndTno=new QuestionAndTno();
        questionAndTno.setSno(sno);
        questionAndTno.setTnos(teachersTno);
        PageHelper.startPage(pageNo, pageNum);
        Page<Question> page = (Page<Question>) questionMapper.getQuestionByTnos(questionAndTno);
        PageBean<Question> pageBean = new PageBean<>();
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        List<Question> allQuestions = page.getResult();
        pageBean.setRecords(allQuestions.stream().collect(Collectors.toList()));
        if (pageBean.getRecords().size() == 0) {
            throw new BusinessException(HttpStatus.NOT_FOUND, "你未选择该教师...");
        }
        return pageBean;
    }

    @Override
    public boolean isTeacherExists(StuAndTnoVo stuAndTnoVo) {
        Integer sno = stuAndTnoVo.getSno();
        if (Objects.isNull(sno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        Integer tno = stuAndTnoVo.getTno();
        if (Objects.isNull(tno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师工号不能为空");
        }
//        List<Question> questionByTno = questionMapper.getQuestionByTno(stuAndTnoVo);
//        if (questionByTno.isEmpty()) {
//            throw new BusinessException(HttpStatus.NOT_FOUND, "你未选择该教师...");
//        }
        return true;
    }

    @Override
    public boolean addQuestion(QuestionVo questionVo) {
        Integer sno1 = questionVo.getSno();
        Question question=new Question();
        question.setSno(sno1);
        Integer sno = question.getSno();
        if (Objects.isNull(sno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "学生学号不能为空");
        }
        Integer tno1 = questionVo.getTno();
        question.setTno(tno1);
        Integer tno = question.getTno();
        if (Objects.isNull(tno)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "教师工号不能为空");
        }
        String ask1 = questionVo.getAsk();
        question.setAsk(ask1);
        String ask = question.getAsk();
        if (StringUtils.isBlank(ask)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "问题不能为空");
        }
        question.setReply("");
        question.setState("未回复");
        boolean r = questionMapper.addQuestion(question);
        if (!r){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "问题插入失败...");
        }
        return true;
    }

    @Override
    public Boolean addReply(AnswerVo answerVo) {
        Boolean aBoolean = questionMapper.addReply(answerVo);
        if (!aBoolean){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "回复问题插入失败...");
        }
        return aBoolean;
    }
    @Override
    public PageBean<Question> getQuestisonsByTno(Integer tno,Integer pageNo,Integer pageNum) {
        PageHelper.startPage(pageNo,pageNum );
        Page<Question> page = (Page<Question>) questionMapper.getQuestionsAndReplyByTno(tno);
        PageBean<Question> pageBean = new PageBean<>();
        int pageCount = page.getPages();
        pageBean.setPageNo(page.getPageNum());
        pageBean.setPageCount(pageCount);
        pageBean.setPageSize(pageNum);
        pageBean.setTotal(page.getTotal());
        List<Question> products = page.getResult();
        pageBean.setRecords(products.stream().collect(Collectors.toList()));
        return pageBean;

    }

    @Override
    public Integer CountQuestionByTno(Integer tno) {
        return questionMapper.CountQuestionByTno(tno);
    }
}
